import { fetchedItem } from "./EngineContext"
import { phaseMin } from "./enum/phase.enum"

declare global {
  interface Window {
    profilingData: {
      phaseTimes: Record<number, number>
      modules: Record<string, any[]>
    }
  }
}
export default class Profiler {
  phaseTimes: Record<number, number>
  modules: Record<string, any[]>
  constructor() {
    this.phaseTimes = []
    this.modules = {}
    this.addAppPhase(phaseMin.UNINITIALIZED)
    window.profilingData = {
      phaseTimes: this.phaseTimes,
      modules: this.modules
    }
  }
  addAppPhase(e) {
    this.phaseTimes[e] = Date.now()
  }
  addModuleLoadTime(name: string, start: number, end: number, fetcheds: fetchedItem[]) {
    const names = Array.from(new Set(fetcheds.map(e => e.moduleName))).join(",")
    const alltime = (fetcheds.reduce((e, t) => e + t.finishAwait - t.startAwait, 0) / (end - start)) * 100
    this.modules[name] = [name, start, end, alltime, names]
  }
}
